package com.edgclub.edg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.Reply;
import com.edgclub.edg.model.Topic;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.Userspan;
import com.edgclub.edg.uc.TextViewFixTouchConsume;
import com.edgclub.edg.uc.TopicReplyDeleteDialog;
import com.edgclub.edg.utils.util.DateUtils;
import com.edgclub.edg.utils.util.DensityUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.rockerhieu.emojicon.EmojiconTextView1;

import java.util.ArrayList;

/**
 */
public class BBMoreReplyAdapter extends GzBaseAdapter{
    private Context mContext;
    private ArrayList<Reply> replyArrayList;

    private int mColor,mColor2;
    private ReplyCommentOnclickListener replyOnclickListener;
    private User user;
    private Topic topic;//

    public BBMoreReplyAdapter(Context context) {
        this.mContext = context;
        replyArrayList = new ArrayList<>();
        mColor = mContext.getResources().getColor(R.color.c1);
        mColor2 = mContext.getResources().getColor(R.color.c6);
        user = new UserPrefUtils(mContext).getUser();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public BBMoreReplyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_bb_more_reply,viewGroup,false);
            return new ViewHolder(view,mListener,mLongClickListener);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder1, final int position) {
        BBMoreReplyAdapter.ViewHolder viewHolder= (ViewHolder) viewHolder1;
        final Reply reply = replyArrayList.get(position);
        boolean isOwner = false;
        if(topic!=null && topic.getUser().getId().equals(reply.getUser().getId())){
            isOwner = true;
        }
        viewHolder.tvreply.setText(getUserSpanString(reply, isOwner));
        viewHolder.tvreply
                .setMovementMethod(TextViewFixTouchConsume.LocalLinkMovementMethod
                        .getInstance());

        if((reply.getUser()!=null && reply.getUser().getId().equals(GuZhiApplication.getInstance().getUserId()))
                || topic.getUser().getId().equals(GuZhiApplication.getInstance().getUserId())){
            viewHolder.imgv_delete.setVisibility(View.VISIBLE);
        }else{
            viewHolder.imgv_delete.setVisibility(View.GONE);
        }
        viewHolder.imgv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteDialog(position,reply);

            }
        });

        viewHolder.tvreply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                replyOnclickListener.replyComment(position,reply);
            }
        });
    }


    private SpannableString getUserSpanString(Reply reply, boolean isOwner) {
        String time = DateUtils
                .getTimestampStringBB(Long.valueOf(reply.getUpdateAt()));
        String owner = "";
//        if(isOwner){
//            owner = " 楼主 ";
//        }
        String replyString =  reply.getUser().getNick()+owner+": "
                + reply.getContent()+"   "+time;
        int timelength = time.length();

        User user = new User();
        String userNick = reply.getUser().getNick();
        user.setId(reply.getUser().getId());

        Userspan userspan = new Userspan("123", mColor, mContext, user);

        SpannableString ss = new SpannableString(replyString);

        ss.setSpan(userspan, 0,  userNick.length()+owner.length()+1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//        if(!TextUtils.isEmpty(owner)){
//            //得到drawable对象，即所要插入的图片
//            Drawable d = mContext.getResources().getDrawable(R.drawable.owner_bg);
//            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//            ss.setSpan(span, userNick.length()+1, userNick.length()+owner.length()-1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        }

//        ReplyDetailCommentSpan replyDetailCommentSpan = new ReplyDetailCommentSpan(mContext,
//                "123", replyOnclickListener, mColor2,position, reply);

        ss.setSpan(new ForegroundColorSpan(mColor2), userNick.length()+owner.length()+1,
                replyString.length()-timelength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(DensityUtils.sp2px(mContext,13)), replyString.length()-timelength, replyString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return ss;
    }

    private void deleteDialog(final int position, final Reply reply) {
        final TopicReplyDeleteDialog dialog = new TopicReplyDeleteDialog(mContext);
        dialog.show();
        dialog.setDeleteClickListener(new TopicReplyDeleteDialog.ReplyDeleteClickListener() {
            @Override
            public void onDeleteListener() {
                //楼层删除
                replyOnclickListener.deleteComment(position,reply);
                dialog.dismiss();
            }
        });
    }
    public interface ReplyCommentOnclickListener {
        public void replyComment(int position,Reply reply);//回复的回复

        public void deleteComment(int position,Reply reply);//删除回复

    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        return replyArrayList.size();
    }

    public ArrayList<Reply> getReplyArrayList() {
        return replyArrayList;
    }

    public void setReplyArrayList(ArrayList<Reply> replyArrayList) {
        this.replyArrayList = replyArrayList;
    }

    public ReplyCommentOnclickListener getReplyOnclickListener() {
        return replyOnclickListener;
    }

    public void setReplyOnclickListener(ReplyCommentOnclickListener replyOnclickListener) {
        this.replyOnclickListener = replyOnclickListener;
    }

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private EmojiconTextView1 tvreply;
        private ImageView imgv_delete;


        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            tvreply = (EmojiconTextView1) view.findViewById(R.id.tvreply);
            imgv_delete = (ImageView) view.findViewById(R.id.imgv_delete);

        }
    }

}
